package com.v1nsai.dataoff;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemSelectedListener;

public class Settings extends Activity implements OnClickListener{
	
	//evil evil global variables
	protected static ToggleButton wifitoggle;
	protected static ToggleButton datatoggle;
	private Spinner wifionspinner;
	private Spinner wifioffspinner;
	private Spinner dataonspinner;
	private Spinner dataoffspinner;
	private SharedPreferences prefs;
	private SharedPreferences.Editor editor;
	private Receivers rcvr;
	protected static final String DATA_KILL = "killdata";
	protected static final String WIFI_KILL = "killwifi";
	protected static final String WIFI_ONDELAY = "wifiondelay";
	protected static final String WIFI_OFFDELAY = "wifioffdelay";
	protected static final String DATA_ONDELAY = "dataondelay";
	protected static final String DATA_OFFDELAY = "dataoffdelay";
	protected static final String PREFS_NAME = "delays";
	private static boolean datastarted = false;
	private static boolean wifistarted = false;
	private static boolean rcvrstarted = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        //call the sharedprefs and prepare an editor to make changes
        prefs = getSharedPreferences(PREFS_NAME, 0);
        editor = prefs.edit();
        
        //initialize and set listener for the buttons
        wifitoggle = (ToggleButton)findViewById(R.id.wifitoggle);
        wifitoggle.setOnClickListener(this);	
        datatoggle = (ToggleButton)findViewById(R.id.datatoggle);
        datatoggle.setOnClickListener(this);
        
        //initialize spinners
        wifionspinner = (Spinner)findViewById(R.id.wifionspinner);
        wifioffspinner = (Spinner)findViewById(R.id.wifioffspinner);
        dataonspinner = (Spinner)findViewById(R.id.dataonspinner);
        dataoffspinner = (Spinner)findViewById(R.id.dataoffspinner);
        
        //set options in the spinners
        ArrayAdapter<CharSequence> wifionadapter = ArrayAdapter.createFromResource(this, R.array.intervals, android.R.layout.simple_spinner_item);
        wifionadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        wifionspinner.setAdapter(wifionadapter);
        
        ArrayAdapter<CharSequence> wifioffadapter = ArrayAdapter.createFromResource(this, R.array.intervals, android.R.layout.simple_spinner_item);
        wifioffadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        wifioffspinner.setAdapter(wifioffadapter);
        
        ArrayAdapter<CharSequence> dataonadapter = ArrayAdapter.createFromResource(this, R.array.intervals, android.R.layout.simple_spinner_item);
        dataonadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataonspinner.setAdapter(dataonadapter);
        
        ArrayAdapter<CharSequence> dataoffadapter = ArrayAdapter.createFromResource(this, R.array.intervals, android.R.layout.simple_spinner_item);
        dataoffadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataoffspinner.setAdapter(dataoffadapter);
        
        //set spinner listeners
        wifioffspinner.setOnItemSelectedListener(new SpinnerListener());
        wifionspinner.setOnItemSelectedListener(new SpinnerListener());
        dataonspinner.setOnItemSelectedListener(new SpinnerListener());
        dataoffspinner.setOnItemSelectedListener(new SpinnerListener());
    }
	@Override
	public void onClick(View v) {
		editor = prefs.edit();
		rcvr = new Receivers();
		
		switch(v.getId())
		{
			case R.id.wifitoggle:
				if(wifitoggle.isChecked() == true){
					editor.putBoolean(WIFI_KILL, true);
					if (rcvrstarted == false ) {
						startService( new Intent(this, Receivers.class ));
						rcvrstarted = true;
					}
					wifistarted = true;
					Toast.makeText(this, "Wifi Kill Service Started", 3 ).show();
					break;
				}
				else if(wifitoggle.isChecked() == false){
					editor.putBoolean(WIFI_KILL, false);
					if (rcvrstarted == true && datastarted == false) {
						rcvr.stopSelf();				//stopService( new Intent( this, Receivers.class )); 
					}
					wifistarted = false;
					Toast.makeText(this, "Wifi Kill Service Stopped", 3 ).show();
					break;
				}
			case R.id.datatoggle:
				if(datatoggle.isChecked() == true) {
					editor.putBoolean(DATA_KILL, true);
					if ( rcvrstarted == false ){
						startService(new Intent(this, Receivers.class));
						rcvrstarted = true;		
					}
					datastarted = true;
					Toast.makeText(this, "Data Kill Service Started", 3).show();
					break;
				}
				else if(datatoggle.isChecked() == false) {
					editor.putBoolean(DATA_KILL, false);
					if (rcvrstarted == true && wifistarted == false) {
						rcvr.stopSelf();             //stopService(new Intent(this, Receivers.class));   
					}
					datastarted = false;
					Toast.makeText(this, "Data Kill Service Stopped", 3).show();
					break;
				}
		}
		editor.commit();
	}
	public class SpinnerListener implements OnItemSelectedListener {

	   public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		   
		   //initialize preference writers
		   prefs = getSharedPreferences(PREFS_NAME, 0);
		   editor = prefs.edit();
		   
		   //set delay for turning wifi on
		   switch(parent.getId())
		   {
		   		case R.id.wifionspinner:
		   			if(parent.getItemAtPosition(pos).toString().equals("0 seconds") ){
			    		editor.putInt(WIFI_ONDELAY, 0);
			    		Toast.makeText(getApplicationContext(), "Wifi enabled delay set to instant", 3).show();
			    		break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("5 seconds") ) {
			    		editor.putInt(WIFI_ONDELAY, 5);
			    		Toast.makeText(getApplicationContext(), "Wifi enabled delay set to 5 seconds", 3).show();
			    		break;
				   	}
				   	else if(parent.getItemAtPosition(pos).toString().equals("10 seconds") ) {				   	
					   	editor.putInt(WIFI_ONDELAY, 10);
					   	Toast.makeText(getApplicationContext(), "Wifi enabled delay set to 10 seconds", 3).show();
					   	break;
				   	}
			    
				   	else if(parent.getItemAtPosition(pos).toString().equals("15 seconds") ){
					   	editor.putInt(WIFI_ONDELAY, 15);
					   	Toast.makeText(getApplicationContext(), "Wifi enabled delay set to 15 seconds", 3).show();
					   	break;
				   	}
				   	else if(parent.getItemAtPosition(pos).toString().equals("30 seconds") ){
					   	editor.putInt(WIFI_ONDELAY, 30);
					   	Toast.makeText(getApplicationContext(), "Wifi enabled delay set to 30 seconds", 3).show();
					   	break;
				   	}
				   	else if(parent.getItemAtPosition(pos).toString().equals("1 minute") ){
					   	editor.putInt(WIFI_ONDELAY, 60);
					   	Toast.makeText(getApplicationContext(), "Wifi enabled delay set to 1 minute", 3).show();
					   	break;
				   	}
				   	else if(parent.getItemAtPosition(pos).toString().equals("5 minutes") ){
					   	editor.putInt(WIFI_ONDELAY, 300);
					   	Toast.makeText(getApplicationContext(), "Wifi enabled delay set to 5 minutes", 3).show();
					   	break;
				   	}
		   			
		   		case R.id.wifioffspinner:
		   			if(parent.getItemAtPosition(pos).toString().equals("0 seconds") ){
					   	editor.putInt(WIFI_OFFDELAY, 0);
					   	Toast.makeText(getApplicationContext(), "Wifi disable delay set to instant", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("5 seconds") ){
					   	editor.putInt(WIFI_OFFDELAY, 5);
					   	Toast.makeText(getApplicationContext(), "Wifi disable delay set to 5 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("10 seconds") ){
					   	editor.putInt(WIFI_OFFDELAY, 10);
					   	Toast.makeText(getApplicationContext(), "Wifi disable delay set to 10 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("15 seconds") ){
					   	editor.putInt(WIFI_OFFDELAY, 15);
					   	Toast.makeText(getApplicationContext(), "Wifi disable delay set to 15 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("30 seconds") ){
					   	editor.putInt(WIFI_OFFDELAY, 30);
					   	Toast.makeText(getApplicationContext(), "Wifi disable delay set to 30 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("1 minute") ){
					   	editor.putInt(WIFI_OFFDELAY, 60);
					   	Toast.makeText(getApplicationContext(), "Wifi disable delay set to 1 minute", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("5 minutes") ){
					   	editor.putInt(WIFI_OFFDELAY, 300);
					   	Toast.makeText(getApplicationContext(), "Wifi disable delay set to 5 minutes", 3).show();
					   	break;
		   			}
		   			
		   		case R.id.dataonspinner:
		   			if(parent.getItemAtPosition(pos).toString().equals("0 seconds") ){
					   	editor.putInt(DATA_ONDELAY, 0);
					   	Toast.makeText(getApplicationContext(), "Data on delay set to instant", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("5 seconds") ){
					   	editor.putInt(DATA_ONDELAY, 5);
					   	Toast.makeText(getApplicationContext(), "Data on delay set to 5 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("10 seconds") ){
					   	editor.putInt(DATA_ONDELAY, 10);
					   	Toast.makeText(getApplicationContext(), "Data on delay set to 10 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("15 seconds") ){
					   	editor.putInt(DATA_ONDELAY, 15);
					   	Toast.makeText(getApplicationContext(), "Data on delay set to 15 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("30 seconds") ){
					   	editor.putInt(DATA_ONDELAY, 30);
					   	Toast.makeText(getApplicationContext(), "Data on delay set to 30 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("1 minute") ){
					   	editor.putInt(DATA_ONDELAY, 60);
					   	Toast.makeText(getApplicationContext(), "Data on delay set to 1 minute", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("5 minutes") ){
					   	editor.putInt(DATA_ONDELAY, 300);
					   	Toast.makeText(getApplicationContext(), "Data on delay set to 5 minutes", 3).show();
					   	break;
		   			}
		   			
		   		case R.id.dataoffspinner:
		   			if(parent.getItemAtPosition(pos).toString().equals("0 seconds") ){
					   	editor.putInt(DATA_OFFDELAY, 0);
					   	Toast.makeText(getApplicationContext(), "Data off delay set to instant", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("5 seconds") ){
					   	editor.putInt(DATA_OFFDELAY, 5);
					   	Toast.makeText(getApplicationContext(), "Data off delay set to 5 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("10 seconds") ){
					   	editor.putInt(DATA_OFFDELAY, 10);
					   	Toast.makeText(getApplicationContext(), "Data off delay set to 10 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("15 seconds") ){
					   	editor.putInt(DATA_OFFDELAY, 15);
					   	Toast.makeText(getApplicationContext(), "Data off delay set to 15 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("30 seconds") ){
					   	editor.putInt(DATA_OFFDELAY, 30);
					   	Toast.makeText(getApplicationContext(), "Data off delay set to 30 seconds", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("1 minute") ){
					   	editor.putInt(DATA_OFFDELAY, 60);
					   	Toast.makeText(getApplicationContext(), "Data off delay set to 1 minute", 3).show();
					   	break;
		   			}
		   			else if(parent.getItemAtPosition(pos).toString().equals("5 minutes") ){
					   	editor.putInt(DATA_OFFDELAY, 300);
					   	Toast.makeText(getApplicationContext(), "Data off delay set to 5 minutes", 3).show();
					   	break;
		   			}
		   			
		   		}
		   editor.commit();
	    }

	    public void onNothingSelected(AdapterView<?> parent) {
	    	editor.putInt(WIFI_ONDELAY, 0);
	    	editor.putInt(WIFI_OFFDELAY, 0);
	    	editor.commit();
	    }
	}
}